using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class MessageDAL
    {
        public static int Save(MessageInfo info)
        {
            string sql = string.Format("insert into [message]([ArticleID],[HeadImg],[UserID],[UserName],[Message],[Praise],[CreateDate]) values(@ArticleID,@HeadImg,@UserID,@UserName,@Message,@Praise,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ArticleID", info.ArticleID),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@Message", info.Message),
                new SqlParameter("@Praise", info.Praise),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar2(sql, paras));
        }
        public static int Update(MessageInfo info)
        {
            string sql = string.Format("update [message] set [ArticleID]=@ArticleID,[HeadImg]=@HeadImg,[UserName]=@UserName,[Message]=@Message,[Praise]=@Praise where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@ArticleID", info.ArticleID),
                new SqlParameter("@HeadImg", info.HeadImg),
                new SqlParameter("@UserName", info.UserName),
                new SqlParameter("@Message", info.Message),
                new SqlParameter("@Praise", info.Praise),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }

        public static MessageInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[message]");
            return Model(dr);
        }
        public static List<MessageInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[message]");
            return ListModel(dt);
        }
        public static List<MessageInfo> FindAllMessage(int articleid, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = "";
            if (articleid != 0)
            {
                filter += " and [message].[ArticleID] =" + articleid;
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[message]", out count);
            return ListModel(dt);
        }
        public static List<MessageInfo> FindMyMessage(int articleid, int startindex, int pagesize, string order, string desc, out int count)
        {
            string filter = "";
            if (B.MyUser.ID != 0)
            {
                filter += " and [message].[UserID] =" + B.MyUser.ID;
            }
            if (articleid!=0)
            {
                filter += " and [message].[ArticleID] =" + articleid;
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[message]", out count);
            return ListModel(dt);
        }
        public static List<MessageInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [message]"));
            return ListModel(dt);
        }
        private static MessageInfo Model(DataRow dr)
        {
            MessageInfo info = new MessageInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.ArticleID = DataConvert.ToInt(dr["ArticleID"]);
                info.HeadImg = DataConvert.ToString(dr["HeadImg"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.UserName = DataConvert.ToString(dr["UserName"]);
                info.Message = DataConvert.ToString(dr["Message"]);
                info.Praise = DataConvert.ToInt(dr["Praise"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                object o = 0;
                try
                {
                    o = CommonDAL.ExecuteScalar(string.Format("select count(1) from praise_message where IsPraise=1 and ArticleID={0} and UserID={1} and MessageID={2}", info.ArticleID, B.MyUser.ID, info.ID));
                }
                catch
                {
                    o = 0;
                }
                info.Ispraise = DataConvert.ToInt(o) > 0 ? true : false;
            }
            return info;
        }
        private static MessageInfo Model(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                return Model(dt.Rows[0]);
            }
            else
            {
                return new MessageInfo();
            }
        }
        private static List<MessageInfo> ListModel(DataTable dt)
        {
            List<MessageInfo> list = new List<MessageInfo>();
            DataColumnCollection col = dt.Columns;
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Model(row));
            }
            return list;
        }
    }
}

